package com.rds.web.controller.pms;

import com.rds.common.core.controller.BaseController;
import com.rds.common.core.domain.AjaxResult;
import com.rds.common.core.domain.entity.PageConfig;
import com.rds.common.core.domain.entity.SysRole;
import com.rds.common.core.domain.entity.SysUsers;
import com.rds.common.core.page.TableDataInfo;
import com.rds.common.core.text.Convert;
import com.rds.common.utils.SecurityUtils;
import com.rds.system.domain.*;
import com.rds.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.*;

/**
 * 参数配置 信息操作处理
 *
 * @author rds
 */
@RestController
@RequestMapping("/pms/pageConfig")
public class PageConfigController extends BaseController
{
    @Autowired
    private IRdaTableColumnService rdaTableColumnService;
    @Autowired
    private IRdaFormDetailsService rdaFormDetailsService;
    @Autowired
    private IRdaCommandService rdaCommandService;
    @Autowired
    private IRdaTypeService rdaTypeService;
    @Autowired
    private IRdaPolicyService rdaPolicyService;
    @Autowired
    private IRdaStateService rdaStateService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IRdaToolbarService rdaToolbarService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysCompanyService syscompany;

    /**
     * 获取页面配置信息
     */
    @GetMapping("/getConfigData")
    public AjaxResult getConfigData(PageConfig pageConfig)
    {
        Map<String,Object> info = new HashMap<>();
        String table= pageConfig.getTable();
        String searchForm= pageConfig.getSearchForm();
        String showForm= pageConfig.getShowForm();
        String command= pageConfig.getCommand();
        String toolbar= pageConfig.getToolbar();
        String treeMenu= pageConfig.getTreeMenu();
        if(table != null && table.length() != 0){
            List<RdaTableColumn> tableColumn = rdaTableColumnService.selectByConfigName(table);
            info.put("tableData",tableColumn);
        }else{
            info.put("tableData",new ArrayList<>());
        }
        if(searchForm != null && searchForm.length() != 0){
            List<RdaFormDetails> formDetails = rdaFormDetailsService.selectByConfigName(searchForm);
            info.put("searchFormData",formDetails);
        }else{
            info.put("searchFormData",new ArrayList<>());
        }
        if(showForm != null && showForm.length() != 0){
            List<RdaFormDetails> showFormDetails = rdaFormDetailsService.selectByConfigName(showForm);
            info.put("showFormData",showFormDetails);
        }else{
            info.put("showFormData",new ArrayList<>());
        }
        if(command != null && command.length() != 0){
            Set<Integer> commandIdSet = new HashSet<>();
            String[] arrayList=command.split(",");
            List<RdaCommand> commandList = new ArrayList<>();
            //如果当前用户是管理员，获取全部
            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                Set<String> roleIdSet = new HashSet<>();
                commandList = rdaCommandService.selectByCommandId(arrayList,roleIdSet,command);
                info.put("commandData",commandList);
            }else{
                //获取当前用户的角色
                Set<String> roleIdSet = SecurityUtils.getRoleIds();
                if(roleIdSet.size()>0){
                    commandList = rdaCommandService.selectByCommandId(arrayList,roleIdSet,command);
                    info.put("commandData",commandList);
                }else{
                    info.put("commandData",new ArrayList<>());
                }
            }
        }else{
            info.put("commandData",new ArrayList<>());
        }
        if(toolbar != null && toolbar.length() != 0){
            List<Object> toolbarData = new ArrayList<>();
            if(!toolbar.equals('0')){
                for(String str:toolbar.split(",")){
                    Map<String,Object> toolbarInfo = new HashMap<>();

                    //如果当前用户是管理员，获取全部
                    if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                        List<RdaCommand> toolbarList = rdaCommandService.selectByToolbar(str,null );
                        toolbarInfo.put("data",toolbarList);
                        toolbarInfo.put("info",rdaToolbarService.selectRdaToolbarByName(str));
                        toolbarData.add(toolbarInfo);
                    }else{
                        //获取当前用户的角色
                        Set<String> roleIdSet = SecurityUtils.getRoleIds();
                        if(roleIdSet.size()>0){
                            List<RdaCommand> toolbarList = rdaCommandService.selectByToolbar(str,roleIdSet);
                            toolbarInfo.put("data",toolbarList);
                            toolbarInfo.put("info",rdaToolbarService.selectRdaToolbarByName(str));
                            toolbarData.add(toolbarInfo);
                        }else{
                            toolbarData.add(toolbarInfo);
                        }
                    }


                }
            }
            info.put("toolbarData",toolbarData);
        }else{
            info.put("toolbarData",new ArrayList<>());
        }
        if(treeMenu != null && treeMenu.length() != 0){
            List<RdaCommand> treeMenuData = rdaCommandService.selectByTreeMenu(treeMenu);
            info.put("treeMenuData",treeMenuData);
        }else{
            info.put("treeMenuData",new ArrayList<>());
        }
        return AjaxResult.success(info);
    }

    /**
     * 获取页面配置信息
     */
    @GetMapping("/getAddForm")
    public AjaxResult getAddForm(PageConfig pageConfig)
    {
        String addForm= pageConfig.getAddForm();
        if(addForm != null && addForm.length() != 0){
            List<RdaFormDetails> addFormDetails = rdaFormDetailsService.selectByConfigName(addForm);
            return AjaxResult.success(addFormDetails);
        }else{
            return AjaxResult.success(new ArrayList<>());
        }
    }
    /**
     * 获取页面配置信息
     */
    @GetMapping("/getTableCol")
    public AjaxResult getTableCol(PageConfig pageConfig)
    {
        String tableCol= pageConfig.getTable();
        if(tableCol != null && tableCol.length() != 0){
            List<RdaTableColumn> tableColumn = rdaTableColumnService.selectByConfigName(tableCol);
            return AjaxResult.success(tableColumn);
        }else{
            return AjaxResult.success(new ArrayList<>());
        }
    }

    /**
     * 获取页面配置信息
     */
    @GetMapping("/getTreeCommand")
    public AjaxResult getTreeCommand(PageConfig pageConfig)
    {
        String command= pageConfig.getCommand();
        if(command != null && command.length() != 0){
            Set<Integer> commandIdSet = new HashSet<>();
            String[] arrayList=command.split(",");
            List<RdaCommand> commandList = new ArrayList<>();
            //如果当前用户是管理员，获取全部
            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                Set<String> roleIdSet = new HashSet<>();
                commandList = rdaCommandService.selectByCommandId(arrayList,roleIdSet,command);
            }else{
                //获取当前用户的角色
                Set<String> roleIdSet = SecurityUtils.getRoleIds();
                if(roleIdSet.size()>0){
                    commandList = rdaCommandService.selectByCommandId(arrayList,roleIdSet,command);
                }
            }
            return AjaxResult.success(commandList);
        }else{
            return AjaxResult.success(new ArrayList<>());
        }
    }

    /**
     * 获取共用的选项信息
     */
    @GetMapping("/getOptionsInfo")
    public AjaxResult getOptionsInfo(RdaType rdaType, RdaPolicy rdaPolicy, RdaState rdaState,SysUsers user,SysRole role)
    {
        Map<String, Object> optionsMap = new HashMap<>();

        List<RdaType> typeList = rdaTypeService.selectRdaTypeList(rdaType);
        List<RdaPolicy> policyList = rdaPolicyService.selectRdaPolicyList(rdaPolicy);
        List<RdaState> stateList = rdaStateService.selectRdaStateList(rdaState);
        List<SysUsers> userList = userService.selectUsersList(user);
        List<SysRole> roleList = roleService.selectRoleList(role);
        List<Map> companyAndDepList=syscompany.selectCompanyListCompanyAndDep();
        for(SysUsers sysUsers:userList){
            Long[] roleIds=Convert.toLongArray(sysUsers.getRoleIdsTemp());
            sysUsers.setRoleIds(roleIds);
        }

        optionsMap.put("typeOptions",typeList);
        optionsMap.put("policyOptions",policyList);
        optionsMap.put("stateOptions",stateList);
        optionsMap.put("userOptions",userList);
        optionsMap.put("roleOptions",roleList);
        optionsMap.put("companyAndDepOptions",companyAndDepList);

        return AjaxResult.success(optionsMap);
    }

    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/userlist")
    public TableDataInfo userlist(SysUsers users)
    {
        startPage();
        List<SysUsers> list = userService.selectUsersList(users);
        return getDataTable(list);
    }
}
